/**
 * 
 */
package com.oscarmovie;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;

import com.oscarmovie.model.MovieRelationship;

/**
 * @author zhengda
 *
 */
public class MovieRelShipUtil {

	private static List<MovieRelationship> movieCache = new ArrayList<MovieRelationship>();
	
	public static List<MovieRelationship> loadAllMovieRelationship(Session session){
		String sql = "select "
		+"         m.id as movieId,               "
		+"         m.name as movieName,           "
		+"         p.id as personId,              "
		+"         p.name as personName,          "
		+"         r.id as roleId,                "
		+"         r.name as roleName,            "
		+"         a.id as awardId,               "
		+"         a.name as awardName,           "
		+"         c.id as calendarId,            "
		+"         c.name as calendarName,        "
		+"         ct.id as countryId,            "
		+"         ct.name as countryName,        "
		+"         cg.id as categoryId,           "
		+"         cg.name as categoryName,       "						
		+"         mpac.type as Type   			"
		+"  from movie m,                 "
		+"       mpr mpr,   "
		+"       person p,                "
		+"       role r,                  "
		+"       mpac mpac,               "
		+"       award a,                 "
		+"       calendar c,              "
		+"       movie_country mc,        "
		+"       country ct,              "
		+"       movie_category mcg,      "
		+"       category cg              "
		+"  where m.id = mpr.movie_id           "
		+"  and mpr.person_id = p.id            "
		+"  and mpr.role_id = r.id              "
		+"  and m.id = mpac.movie_id            "
		+"  and mpac.award_id = a.id            "
		+"  and mpac.calendar_id = c.id         "
		+"  and m.id = mc.movie_id              "
		+"  and mc.country_id = ct.id           "
		+"  and m.id = mcg.movie_id             "
		+"  and mcg.category_id = cg.id        ";
		
		movieCache = session.createSQLQuery(sql).
		addScalar("movieId", Hibernate.INTEGER).
		addScalar("movieName", Hibernate.STRING).
		addScalar("personId", Hibernate.INTEGER).
		addScalar("personName", Hibernate.STRING).
		addScalar("roleId", Hibernate.INTEGER).
		addScalar("roleName", Hibernate.STRING).
		addScalar("awardId", Hibernate.INTEGER).
		addScalar("awardName", Hibernate.STRING).
		addScalar("calendarId", Hibernate.INTEGER).
		addScalar("calendarName", Hibernate.STRING).
		addScalar("countryId", Hibernate.INTEGER).
		addScalar("countryName", Hibernate.STRING).
		addScalar("categoryId", Hibernate.INTEGER).
		addScalar("categoryName", Hibernate.STRING).
		addScalar("Type", Hibernate.INTEGER).
		setResultTransformer(Transformers.aliasToBean(MovieRelationship.class)).  		
		setCacheable(true).list();
		
		return movieCache;
	}		
	
	public static Set<MovieRelationship> filterMovie(int calendarId, int categoryId, int countryId, int awardId, int personId, int type) {
		Set<MovieRelationship> filteredMovies = new HashSet<MovieRelationship>(); 				
		for (MovieRelationship mrs: movieCache) {
			if ((calendarId == 0 ? true: mrs.getCalendarId() == calendarId) &&					
				(categoryId == 0 ? true : mrs.getCategoryId() == categoryId) && 
				(countryId == 0 ? true : mrs.getCountryId() == countryId) && 				
				(awardId == 0 ? true : mrs.getAwardId() == awardId) && 
				(personId == 0 ? true :  mrs.getPersonId() == personId) && 
				(type == 0 ? true : mrs.getType() == type)
			) {
				filteredMovies.add(mrs);
			}
		}		
		return filteredMovies;
	}
	
	public static String getMovieIdString(int calendarId, int categoryId, int countryId, int awardId, int personId, int type) {
		StringBuilder sb = new StringBuilder();
		Set<MovieRelationship> filteredMovies = filterMovie(calendarId, categoryId, countryId,awardId, personId, type);		
		for (MovieRelationship mrs : filteredMovies) {
			sb.append(mrs.getMovieId()).append(",");
		}		
    sb.append("0");
		
		return sb.toString();	
	}
			
}
